<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>鸡兔同笼问题</title>
<style>
    .container {
        width: 60%;
        margin: 0 auto;
        background-color: aqua;
        min-height: 220px;
    }

    h2 {
        text-align: center;
        padding-top: 10px;
    }

    p {
        width: 90%;
        margin: 0 auto;
        text-align: center;
    }

    input[type="number"] {
        width: 50px;
        text-align: center;
    }

    button {
        display: block;
        margin: 20px auto;
        padding: 10px 20px;
        font-size: 16px;
    }

    #result {
        margin-top: 20px;
        font-weight: bold;
    }
</style>
</head>
<body>
<div class="container">
    <h2>循环解决鸡兔同笼问题</h2>
    <p>
        鸡兔一共 <input type="number" id="totalAnimals" value="35"> 只。
        笼子里脚一共 <input type="number" id="totalLegs" value="94"> 只。
    </p>
    <button onclick="calculate()">计算</button>
    <p id="result"></p>
</div>

<script>
function calculate() {
    const totalAnimals = parseInt(document.getElementById('totalAnimals').value);
    const totalLegs = parseInt(document.getElementById('totalLegs').value);
    let chickens, rabbits;

    if (totalLegs % 2 !== 0) {
        document.getElementById('result').textContent = '无解：脚的数量必须是偶数。';
        document.getElementById('result').style.color = 'red';
        return;
    }

    rabbits = (totalLegs - 2 * totalAnimals) / 2;
    chickens = totalAnimals - rabbits;

    if (rabbits < 0 || chickens < 0) {
        document.getElementById('result').textContent = '无解：输入的动物或脚的数量不合理。';
        document.getElementById('result').style.color = 'red';
    } else {
        document.getElementById('result').textContent = `有解：鸡有 ${chickens} 只，兔子有 ${rabbits} 只。`;
        document.getElementById('result').style.color = 'green';
    }
}
</script>
</body>
</html>
